
using System;
using System.Text;
using LastFMGenNHibernate.CEN;
using NHibernate;
using NHibernate.Cfg;
using NHibernate.Criterion;
using NHibernate.Exceptions;
using LastFMGenNHibernate.EN.LastFM;

namespace LastFMGenNHibernate.CAD.LastFM
{
        public partial class ComentarioCAD : BasicCAD, IComentarioCAD
        {
        public ComentarioCAD() : base ()
        {
        }

        public ComentarioCAD(ISession sessionAux) : base (sessionAux)
        {
        }



        public ComentarioEN ReadOIDDefault (int id)
        {
                ComentarioEN comentarioEN = null;

                try
                {
                        SesssionInitializeTransaction ();
                        comentarioEN = (ComentarioEN)session.Load (typeof(ComentarioEN),
                                id

                                );
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in ComentarioCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return comentarioEN;
        }


        public int New_ (ComentarioEN comentario)
        {
                try
                {
                        SesssionInitializeTransaction ();

                        session.Save (comentario);
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in ComentarioCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return comentario.Id;
        }

        public void Destroy (int id)
        {
                try
                {
                        SesssionInitializeTransaction ();
                        ComentarioEN comentarioEN = (ComentarioEN)session.Load (typeof(ComentarioEN),
                                id

                                );
                        session.Delete (comentarioEN);
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in ComentarioCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }
        }

        public System.Collections.Generic.IList<ComentarioEN> DevolverComentarios (int first, int size)
        {
                System.Collections.Generic.IList<ComentarioEN> result = null;
                try
                {
                        SesssionInitializeTransaction ();
                        if (size > 0)
                                result = session.CreateCriteria (typeof(ComentarioEN)).
                                         SetFirstResult (first).SetMaxResults (size).List<ComentarioEN>();
                        else
                                result = session.CreateCriteria (typeof(ComentarioEN)).List<ComentarioEN>();
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in ComentarioCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }

                return result;
        }

        public void Modify (ComentarioEN comentario)
        {
                try
                {
                        SesssionInitializeTransaction ();
                        ComentarioEN comentarioEN = (ComentarioEN)session.Load (typeof(ComentarioEN), comentario.Id);

                        comentarioEN.Comment = comentario.Comment;

                        session.Update (comentarioEN);
                        SessionCommit ();
                }

                catch (Exception ex) {
                        SessionRollBack ();
                        if (ex is ModelException)
                                throw ex;
                        throw new DataLayerException ("Error in ComentarioCAD.", ex);
                }


                finally
                {
                        SessionClose ();
                }
        }
        }
}
